package org.leyi.shop.config;

import lombok.Data;
import org.apache.commons.lang3.StringUtils;
import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.config.Config;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 *
 * description:  TODO
 *<br> create 2024/5/5-15:55
 * @author  lenovo
 */
@Configuration
@Data
public class RedissonConfig {
    @Value("${spring.data.redis.host}")
    private String host;
    @Value("${spring.data.redis.port}")
    private String port;
    @Value("${spring.data.redis.password}")
    private String password;
    @Value("${redisson.database}")
    private String redissonDatabase;
    @Bean
    public RedissonClient redissonClient(){
        Config config = new Config();
        String address = String.format("redis://%s:%s",host, port);
        config.useSingleServer().setPassword(password)
                .setAddress(address).setDatabase(Integer.parseInt(StringUtils.isBlank(redissonDatabase) ?"14":redissonDatabase));
        config.setCodec(new org.redisson.codec.JsonJacksonCodec());
        return Redisson.create(config);
    }
}
